package com.social.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.social.common.Result;
import com.social.dto.AdminUserDto;
import com.social.entity.AdminUser;
import com.social.entity.HouseKeeper;
import com.social.loginFilter.WebSecurityConfig;
import com.social.service.adminUser.AdminUserService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.social.common.Tool.getIdByUUId;
/*
* 管理员控制层
* */

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminUserService adminUserService;
    @RequestMapping(value = "/index")
    public String index(){
        return "admin/index";
    }
    @RequestMapping(value = "/toLogin")
    public String toLogin(){
        return "admin/login";
    }
    @RequestMapping("/login")
    public String loginPost(String account, String password, HttpSession session,Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("name",account);
        map.put("password",password);


        AdminUser adminUser=adminUserService.login(map);
        if(adminUser!=null){
        // 设置session
            session.setAttribute(WebSecurityConfig.SESSION_KEY, adminUser);
            String role=adminUser.getRole();
            if(role.equals("0")){
                role="超级管理员";
            }
            if(role.equals("1")){
                role="二级管理员";
            }
            if(role.equals("2")){
                role="三级管理员";
            }
            if(role.equals("3")){
                role="四级管理员";
            }
            session.setAttribute("role", role);
            map.put("success", true);
            map.put("message", "登录成功");
        }else{
            model.addAttribute("message","用户名或密码错误！");
            map.put("success", false);
            map.put("message", "密码错误");
            return "admin/login";
        }
        return "admin/index";
    }
    @RequestMapping(value = "/welcome")
    public String welcome(){
        return "admin/welcome";
    }

    @RequestMapping(value = "/admin-role")
    public String adminRole(){
        return "admin/admin-role";
    }
    @RequestMapping(value = "/admin-role-add")
    public String adminRoleAdd(){
        return "admin/admin-role-add";
    }

    @RequestMapping(value = "/admin-permission")
    public String adminPermission(){
        return "admin/admin-permission";
    }
    @RequestMapping(value = "/admin-permission-add")
    public String adminPermissionAdd(){
        return "admin/admin-permission-add";
    }

    @RequestMapping(value = "/admin-list")
    public String adminList(){
        return "admin/admin-list";
    }
    @ResponseBody
    @RequestMapping(value = "/list")
    public String list(AdminUserDto adminUserDto,Model model){
        int currPage = adminUserDto.getCurrPage();
        int pageSize = adminUserDto.getPageSize();
        PageHelper.startPage(currPage, pageSize);
        List<AdminUser> adminUserList = adminUserService.selectByAll();

        Result result=new Result();
        result.setRows(adminUserList);
        PageInfo<AdminUser> pageInfo=new PageInfo<>(adminUserList);
        result.setTotal(pageInfo.getTotal());

        JSONObject result2 = new JSONObject();
        result2.put("msg", "ok");
        result2.put("result", true);
        result2.put("method", "list");
        result2.put("datas", adminUserList);
        result2.put("currPage", currPage);
        result2.put("pageSize",pageSize);
        result2.put("total",pageInfo.getTotal());

        System.out.println("result2=="+result2.toJSONString());
        return result2.toJSONString();
    }
    @RequestMapping(value = "/admin-add")
    public String adminAdd(){
        return "admin/admin-add";
    }

    @ResponseBody
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public String add(String name ,String password1,String password2,String role){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("name=="+name);
        System.out.println("password1=="+password1);
        System.out.println("password2=="+password2);
        System.out.println("role=="+role);
        JSONObject result = new JSONObject();
        if(password1!=null&&password1!=""&&password1.equals(password2)&&name!=null&&name!=""){
            AdminUser adminUser = new AdminUser();
            adminUser.setId(getIdByUUId());
            adminUser.setName(name);
            adminUser.setPassword(password1);
            adminUser.setRole(role);
            adminUser.setDate(new Date());
            int count =adminUserService.insert(adminUser);
            result.put("msg", "ok");
            result.put("result", true);
            result.put("count", count);
        }else {
            result.put("msg", "not ok");
            result.put("result", false);
        }
        System.out.println("result=="+result.toJSONString());
        return result.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public String update(String name ,String password1,String password2,String role,String id,String oldpassword){

        System.out.println("id=="+id);
        JSONObject result = new JSONObject();
        Map<String,Object> params =new HashMap();
        params.put("id",id);
        AdminUser adminUser= adminUserService.selectByPrimaryKey(params);
        if(!adminUser.getPassword().equals(oldpassword)){
            result.put("result", false);
            result.put("msg", "原始密码输入错误！");
            return result.toJSONString();
        }

        if(password1!=null&&password1!=""&&password1.equals(password2)){
            adminUser.setPassword(password1);
        }
        if(name!=null&&name!=""){
            adminUser.setName(name);
        }
        if(role!=null&&role!=""){
            adminUser.setRole(role);
        }
        int count =adminUserService.updateByPrimaryKeySelective(adminUser);
        if(count>0){
            result.put("msg", "ok");
            result.put("result", true);
        }else{
            result.put("msg", "not ok");
            result.put("result", false);
        }

        return result.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/show", produces = {"application/json;charset=UTF-8"})
    public String show(String id){
        JSONObject result = new JSONObject();
        Map<String,Object> params =new HashMap();
        params.put("id",id);
        AdminUser adminUser= adminUserService.selectByPrimaryKey(params);
            result.put("adminUser", adminUser);
            result.put("result", true);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public String del(String id){
        JSONObject result = new JSONObject();
        int count=adminUserService.deleteByPrimaryKey(id);
        if(count>0){
            result.put("result", true);
        }else{
            result.put("result", false);
        }
        return result.toJSONString();
    }
    @RequestMapping(value = "/toShow")
    public String toShow(){
        return "admin/admin-show";
    }
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(String id,Model model){
        Map<String,Object> params =new HashMap();
        params.put("id",id);
        AdminUser adminUser= adminUserService.selectByPrimaryKey(params);
        model.addAttribute("adminUser",adminUser);
        return "admin/admin-update";
    }


}
